package com.xzm;

import java.io.Serializable;

public class HungrySingletonTest{
    public static void main(String[]args){
        new Thread(()->{
            HungerySingleton instance1 = HungerySingleton.getInstance();
            System.out.println(instance1);
        }).start();
        new Thread(()->{
            HungerySingleton instance1 = HungerySingleton.getInstance();
            System.out.println(instance1);
        }).start();
    }
}

class HungerySingleton implements Serializable{
    private static HungerySingleton instance = new HungerySingleton();
    private HungerySingleton(){
        if(instance!=null){
            throw new RuntimeException("已经有实例化对象。");
        }
    }
    public   static HungerySingleton getInstance(){
        return instance;
    }
    private Object readResolve() {
        return instance;
    }
}
